<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\matrix\controller; //Demo插件英文名，改成你的插件英文就行了
use cmf\controller\PluginMatrixRestBaseController;
use think\Db;
use think\Validate;

/**
 * api_charroom
 * Class ApiCharroomController
 * @package plugins\matrix\controller
 */
class ApiCharroomController extends PluginMatrixRestBaseController
{
    /**
     * 构造函数
     */
    public function _initialize()
    {
        if (empty($this->user)) {
            $this->error(['code' => 10000, 'msg' => '未登录']);
        }
    }

    public function index()
    {
        $department_id = $this->request->param("department_id");
        if(empty($department_id)){
            $paginate = Db::name('matrix_charroom')
                ->order("create_time DESC")
                ->paginate(10);
        }else{
            $where["department_id"] = $department_id;
            $paginate = Db::name('matrix_charroom')
                ->where($where)
                ->order("create_time DESC")
                ->paginate(10);
        }

        $list = $paginate->items();
        if(count($list) > 0){
            foreach ($list as $key => &$val) {
                $val['create_time'] = date("Y-m-d h:i",$val['create_time']);
            }
            $this->success('success',array("total_count"=>$paginate->total(),"list"=>$list));
        }else{
            $this->successcode(2,"暂无数据");
        }
    }






    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'talker'          => 'require',
                'department_id'          => 'require'
            ]);
            $validate->message([
                'talker.require'          => '群组编号不能为空!',
                'department_id.require'          => '部门ID不能为空!'
            ]);

            $data = $this->request->param();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }else{
                $department =  DB::name('matrix_department')->where(array("id"=>$data["department_id"]))->find();
                $info["department_id"] = $department["id"];
                $info["department_name"] = $department["name"];
                $info["update_time"] = time();
                $result  = DB::name('matrix_charroom')->where(array("talker"=>$data["talker"]))->update($info);
                if ($result !== false) {
                    $this->success("修改成功!");
                } else {
                    $this->error("修改失败！");
                }
            }
        }else{
            $this->error("参数异常！");
        }
    }

    public function delete()
    {
        $validate = new Validate([
            'talker'          => 'require'
        ]);
        $validate->message([
            'talker.require'          => '群组编号不能为空!'
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }else{
            $result = DB::name('matrix_charroom')->where(["talker" => $data["talker"]])->delete();
            if ($result !== false) {
                $this->success("删除成功!");
            } else {
                $this->error("删除失败！");
            }
        }
    }

}
